using System;
using System.Web.UI.WebControls;

namespace System.Web.Core 
{
    public class DefaultButtonTextBox : TextBox
    {
        private const string OnKeyDownButtonAttribute = "KeyDownHandler{0}(event);";

        public DefaultButtonTextBox()
        {

        }

        public string ButtonId
        {
            get
            {
                return (((string)this.ViewState["ButtonId"]) ?? string.Empty);
            }
            set
            {
                this.ViewState["ButtonId"] = value;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            //IButton button = CSControlUtility.Instance().FindButtonControl(this, this.ButtonId);
            //if (button != null)
            //{
            //    this.RegisterButtonScript(button);
            //    base.Attributes.Add("onkeydown", string.Format("KeyDownHandler{0}(event);", button.Control.ClientID));
            //}
            base.OnPreRender(e);
        }

        protected void RegisterButtonScript(IButton button)
        {
            //StringBuilder builder = new StringBuilder();
            //builder.Append("<script type=\"text/javascript\">\n");
            //builder.Append("<!--\n");
            //builder.Append("function KeyDownHandler");
            //builder.Append(button.Control.ClientID);
            //builder.Append("(event)\n");
            //builder.Append("{\n");
            //builder.Append("\tif (event.keyCode == 13)\n");
            //builder.Append("\t{\n");
            //builder.Append("\t\tevent.returnValue = false;\n");
            //builder.Append("\t\tevent.cancel = true;\n");
            //builder.Append("     ");
            //builder.Append(this.Page.ClientScript.GetPostBackEventReference(button.Control, ""));
            //builder.Append("\t}\n");
            //builder.Append("}\n");
            //builder.Append("\n");
            //builder.Append("//-->\n");
            //builder.Append("</script>\n");

            //Page.ClientScript.RegisterStartupScript(this.GetType(), button.Control.ClientID, builder.ToString());
            //Page.RegisterRequiresPostBack(this);

        }

    }
}
